package commonInfrastructure.menu.managedbean;

import commonInfrastructure.accountManagement.entity.EmployeeEntity;
import commonInfrastructure.accountManagement.session.EmployeeSessionBean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.submenu.Submenu;
import org.primefaces.model.DefaultMenuModel;
import org.primefaces.model.MenuModel;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

@ManagedBean
@ViewScoped
public class Menu2ManagedBean implements Serializable {

    private MenuModel model;
    @EJB
    EmployeeSessionBean employee;

    public Menu2ManagedBean() {
    }

    public MenuModel getModel() {
//            if(model == null)
//            {
        model = new DefaultMenuModel();

        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Boolean isLogin = (Boolean) request.getSession().getAttribute("isLogin");


        if (isLogin == true) {
            Long loginId = (Long) request.getSession().getAttribute("userId");
            EmployeeEntity user = employee.getEmployeeById(loginId);
            List<String> userType = new ArrayList<String>();

            for (int i=0;i<user.getRoles().size();i++){
                userType.add(user.getRoles().get(i).getRoleName());
            }

            //First submenu
            Submenu submenu = new Submenu();
            MenuItem item = new MenuItem();
            
            
            if (userType.contains("Salesman")||userType.contains("Sales Manager")) {
                
                submenu.setLabel("Client Management");
                submenu.setIcon("ui-icon ui-icon-flag");
                
                item.setValue("Register New Client");
                item.setUrl("/crm/clientManagement/createNewCompany.xhtml");
                submenu.getChildren().add(item);
                
                item = new MenuItem();
                item.setValue("Manage Clients");
                item.setUrl("/crm/clientManagement/deleteCompany.xhtml");
                submenu.getChildren().add(item);
                
                model.addSubmenu(submenu);
                
                submenu = new Submenu();
                submenu.setLabel("Sales Inquiry");
                submenu.setIcon("ui-icon ui-icon-comment");
                
                item = new MenuItem();
                item.setValue("Create New");
                item.setUrl("/crm/salesOrderProcessing/createSalesInquiry.xhtml");
                submenu.getChildren().add(item);
                
                item = new MenuItem();
                item.setValue("Manage Inquiries");
                item.setUrl("/crm/salesOrderProcessing/deleteSalesInquiry.xhtml");
                submenu.getChildren().add(item);
                
                model.addSubmenu(submenu);

                submenu = new Submenu();
                submenu.setLabel("Sales Quotation");
                submenu.setIcon("ui-icon ui-icon-note");
                
                item = new MenuItem();
                item.setValue("Create New");
                item.setUrl("/crm/salesOrderProcessing/createSalesQuotation.xhtml");
                submenu.getChildren().add(item);
                
                item = new MenuItem();
                item.setValue("Raise From Inquiry");
                item.setUrl("/crm/salesOrderProcessing/inquiryToQuotation.xhtml");
                submenu.getChildren().add(item);
                
                item = new MenuItem();
                item.setValue("Manage Quotations");
                item.setUrl("/crm/salesOrderProcessing/deleteSalesQuotation.xhtml");
                submenu.getChildren().add(item);
                
                model.addSubmenu(submenu);

                submenu = new Submenu();
                submenu.setLabel("Sales Order");
                submenu.setIcon("ui-icon ui-icon-cart");
                
                item = new MenuItem();
                item.setValue("Raise New");
                item.setUrl("/crm/salesOrderProcessing/convertSalesQuotation.xhtml");
                submenu.getChildren().add(item);
                
                item = new MenuItem();
                item.setValue("View All Sales Order");
                item.setUrl("/crm/salesOrderProcessing/viewAllSalesOrder.xhtml");
                submenu.getChildren().add(item);
                
                model.addSubmenu(submenu);

            }
            
            if (userType.contains("Admin")) {
                    
                submenu = new Submenu();
                submenu.setLabel("Account");
                submenu.setIcon("ui-icon ui-icon-contact");
                
                item = new MenuItem();
                item.setValue("Create New");
                item.setUrl("/accountManagement/AddEmployee.xhtml");
                submenu.getChildren().add(item);
                
                item = new MenuItem();
                item.setValue("Manage Employees");
                item.setUrl("/accountManagement/ManageEmployee.xhtml");
                submenu.getChildren().add(item);
                
                model.addSubmenu(submenu);
                
                submenu = new Submenu();
                submenu.setLabel("Role");
                submenu.setIcon("ui-icon ui-icon-star");
                
                item = new MenuItem();
                item.setValue("Create New");
                item.setUrl("/accountManagement/AddRole.xhtml");
                submenu.getChildren().add(item);
                
                item = new MenuItem();
                item.setValue("Manage Roles");
                item.setUrl("/accountManagement/ManageRole.xhtml");
                submenu.getChildren().add(item);
                
                model.addSubmenu(submenu);
                
                submenu = new Submenu();
                submenu.setLabel("Functionality");
                submenu.setIcon("ui-icon ui-icon-wrench");
                
                item = new MenuItem();
                item.setValue("Create New");
                item.setUrl("/accountManagement/AddFunctionality.xhtml");
                submenu.getChildren().add(item);
                
                item = new MenuItem();
                item.setValue("Manage Functionality");
                item.setUrl("/accountManagement/ManageFunctionality.xhtml");
                submenu.getChildren().add(item);
                
                model.addSubmenu(submenu);
            }
            
            if (userType.contains("Finance Personnel")) {
                    
                submenu = new Submenu();
                submenu.setLabel("Invoice");
                submenu.setIcon("ui-icon ui-icon-document");
                
                item = new MenuItem();
                item.setValue("Create New");
                item.setUrl("/frm/billingAndPaymentProcess/createInvoice.xhtml");
                submenu.getChildren().add(item);
                
                item = new MenuItem();
                item.setValue("View Invoice");
                item.setUrl("/frm/billingAndPaymentProcess/viewAllInvoices.xhtml");
                submenu.getChildren().add(item);
                
                item = new MenuItem();
                item.setValue("Commence Dunning Email");
                item.setUrl("/frm/billingAndPaymentProcess/commenceDunningEmail.xhtml");
                submenu.getChildren().add(item);
                
                model.addSubmenu(submenu);
                
                submenu = new Submenu();
                submenu.setLabel("Receitp");
                submenu.setIcon("ui-icon ui-icon-document-b");
                
                item = new MenuItem();
                item.setValue("View Receipt");
                item.setUrl("/frm/billingAndPaymentProcess/viewAllReceipt.xhtml");
                submenu.getChildren().add(item);
                
                model.addSubmenu(submenu);
            }
            
        }
        return model;
    }
    //}
}
